<template>
  <div class="app-container">
    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="580px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        label-width="98px"
      >
        <el-col :span="24">
          <el-form-item label="扫码日期">
            <el-date-picker
              v-model="daterangeColmoRq"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              :default-time="['00:00:00', '23:59:59']"
              type="datetimerange"
              range-separator="-"
              style="width: 400px"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="综合查询" prop="colmoPmgg">
            <el-input
              v-model="queryParams.colmoPmgg"
              placeholder="请输入客户编码或品名规格"
              clearable
              style="width: 400px"
              type="textarea"
              size="small"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          @click="handleSearch"
          size="small"
          >查询</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['production:colmoReport:export']"
          @click="handleExport(1)"
          size="small"
        >
          明细数据导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          v-hasPermi="['production:colmoReport:export']"
          @click="handleExport(2)"
          size="small"
        >
          物料统计导出
        </el-button>
      </el-col>
    </el-row>
    <el-tabs v-model="activeTab">
      <el-tab-pane label="明细数据" name="first">
        <el-table
          border
          stripe
          :height="tableHChild"
          @scrollBottom="load"
          :data="colmoDetailList"
          id="exportPzMxTj"
          :header-cell-style="{ 'text-align': 'center' }"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="itmWldm" />
          <el-table-column
            label="客户编码"
            align="center"
            prop="colmoCustwldm"
          />
          <el-table-column
            label="品名规格"
            align="center"
            width="480"
            prop="colmoPmgg"
          />
          <el-table-column label="生产机台" align="center" prop="colmoBc" />
          <el-table-column
            label="扫码人员"
            width="80"
            :show-overflow-tooltip="true"
            align="center"
            prop="colmoJlry"
          />
          <el-table-column
            label="过账日期"
            width="150"
            :show-overflow-tooltip="true"
            align="center"
            prop="colmoJlrq"
          >
            <template #default="scope">
              <span>{{
                parseTime(scope.row.colmo_jlrq, "{y}-{m}-{d} {h}:{i}:{s}")
              }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="扫码编号"
            width="230"
            align="center"
            prop="colmoCarBh"
          />
        </el-table>
      </el-tab-pane>
      <el-tab-pane label="按物料统计" name="second">
        <el-table
          border
          stripe
          @scrollBottom="load2"
          id="exportwldmTj"
          :height="tableHChild"
          :data="colmoWldmList"
        >
          <el-table-column
            label="#"
            type="index"
            width="55"
            align="center"
            fixed="left"
          />
          <el-table-column label="物料代码" align="center" prop="itmWldm" />
          <el-table-column
            label="客户编码"
            align="center"
            prop="colmoCustwldm"
          />
          <el-table-column label="品名规格" align="center" prop="colmoPmgg" />
          <el-table-column label="扫码计数" align="center" prop="colmoNumber" />
          <el-table-column label="生产机台" align="center" prop="colmoBc" />
        </el-table>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import { colmoReport, exportColmoDet } from "@/api/production/colmoDet";

export default {
  name: "ColmoReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      tableH: (document.body.clientHeight - 130) / 2,
      tableHChild: document.body.clientHeight - 280,
      activeTab: "first",
      colmoDetailList: [],
      colmoWldmList: [],
      // 显示搜索条件
      openSearch: false,
      // 总条数
      total: 0,
      total2: 0,
      // 弹出层标题
      title: "",
      // 建立日期时间范围
      daterangeColmoRq: [
        this.$moment().startOf("month").toDate(),
        this.$moment().endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 40,
        colmoPmgg: null,
        index: 1,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询来料进货入库开单列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeColmoRq && "" != this.daterangeColmoRq) {
        this.queryParams.params["beginDate"] = this.moment(
          this.daterangeColmoRq[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endDate"] = this.moment(
          this.daterangeColmoRq[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      colmoReport(this.queryParams).then((response) => {
        this.colmoDetailList = [];
        this.colmoWldmList = [];

        //明细
        this.colmoDetailList = response.data.list1.rows;
        this.total = response.data.list1.total;
        //物料
        this.colmoWldmList = this.colmoWldmList.concat(
          response.data.list2.rows
        );
        this.total2 = response.data.list2.total;

        this.loading = false;
      });
    },
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        colmoReport(this.queryParams).then((response) => {
          //明细
          this.colmoDetailList = this.colmoDetailList.concat(
            response.data.list1.rows
          );
          this.total = response.data.list1.total;
          this.loading = false;
        });
      }
      return 0;
    },
    load2() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total2 / this.queryParams.pageSize)
      ) {
        this.queryParams.pageNum++;
        colmoReport(this.queryParams).then((response) => {
          //明细
          this.colmoWldmList = this.colmoWldmList.concat(
            response.data.list2.rows
          );
          this.total2 = response.data.list2.total;
          this.loading = false;
        });
      }
      return 0;
    },
    handleExport(index) {
      const queryParams = this.queryParams;
      queryParams.index = index;
      this.$confirm("是否确认导出所有Colmo高端生产统计分析?", "警告", {
        cancelButtonText: "取消",
        confirmButtonText: "确定",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportColmoDet(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
    handleSearch() {
      this.openSearch = true;
      this.title = "查询Colmo高端生产";
    },
    // 取消按钮
    cancel() {
      this.openSearch = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.openSearch = false;
    },
  },
};
</script>
